package com.boot.web.controller;

import java.util.List;
import java.util.Map;

import com.boot.web.dao.GoodsOrderDetailsDao;
import com.boot.web.dao.GoodsOrderTradesDao;
import com.boot.web.dao.GoodsOrdersDao;
import com.boot.web.dto.GoodsOrdersDTO;
import com.boot.web.entity.GoodsOrderDetails;
import com.boot.web.entity.GoodsOrderTrades;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.GoodsOrders;
import com.boot.web.service.GoodsOrdersService;

/**
 * 商品订单控制器
 * @author 【zhouJie】
 * @date 2021-09-28 17:52:24
 */
@RestController
@RequestMapping("/goodsOrders")
public class GoodsOrdersController {

    @Autowired
    private GoodsOrdersService goodsOrdersService;

    @Autowired
    private GoodsOrdersDao goodsOrdersDao;

    @Autowired
    private GoodsOrderTradesDao goodsOrderTradesDao;

    @Autowired
    private GoodsOrderDetailsDao goodsOrderDetailsDao;

    @PostMapping("/save")
    public ResponseServer save(GoodsOrders goodsOrders) {
        return ResponseServer.success("添加成功", goodsOrdersService.save(goodsOrders));
    }

    @PutMapping("/update")
    public ResponseServer update(GoodsOrdersDTO goodsOrders) {
        if(!"".equals(goodsOrders.getLogisticsNumber()) && goodsOrders.getLogisticsNumber() != null){
            GoodsOrderTrades trades = goodsOrderTradesDao.queryByTradeNo(goodsOrders.getTradeNo());
            trades.setLogisticsNumber(goodsOrders.getLogisticsNumber());
            if("1".equals(trades.getPayStatus()) && "1".equals(trades.getDealStatus()) && "0".equals(trades.getRefundStatus())){
                trades.setDealStatus("2");
            }
            goodsOrderTradesDao.update(trades);
        }
        GoodsOrders orders = goodsOrdersDao.queryByOrderNo(goodsOrders.getOrderNo());
        orders.setConsigneeName(goodsOrders.getConsigneeName());
        orders.setConsigneePhone(goodsOrders.getConsigneePhone());
        orders.setAddress(goodsOrders.getAddress());
        goodsOrdersDao.update(orders);
        return ResponseServer.success("修改成功");
    }

    @PutMapping("/getBuyInfo")
    public ResponseServer getBuyInfo(GoodsOrdersDTO goodsOrders) {
        List<GoodsOrderDetails> detailsList = goodsOrderDetailsDao.queryByTradeNo(goodsOrders.getTradeNo());
        return ResponseServer.success("获取商品详情成功", detailsList);
    }

    @DeleteMapping("/delete/{tradeId}")
    public ResponseServer delete(@PathVariable Integer tradeId) {
        GoodsOrderTrades trades = goodsOrderTradesDao.queryById(tradeId);
        trades.setIsDel("1");
        goodsOrderTradesDao.update(trades);
        return ResponseServer.success("删除成功");
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        if(ids.length > 0){
            for(int i = 0; i < ids.length; i++){
                GoodsOrderTrades trades = goodsOrderTradesDao.queryById(ids[i]);
                trades.setIsDel("1");
                goodsOrderTradesDao.update(trades);
            }
        }
        return ResponseServer.success("批量删除成功");
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(goodsOrdersDao.queryGoodsOrdersVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(goodsOrdersService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(goodsOrdersService.getById(id));
    }

    /**
     * 上传物流单号-蜜淘
     * @param params
     * @return
     */
    @PostMapping("/uploadLogisticsNumber_beeAmoy")
    public ResponseServer uploadLogisticsNumber_beeAmoy(@RequestParam Map<String, Object> params) {
        String strJson = params.get("readList").toString();
        JSONArray jsonArray = JSONArray.fromObject(strJson);
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String tradeNo = jsonObject.get("交易号").toString().replaceAll("\\s*", "");
                GoodsOrderTrades order1 = goodsOrderTradesDao.queryByTradeNo(tradeNo);
                if (order1 != null) {
                    if(jsonObject.containsKey("物流号")){
                        String logisticsNumber = jsonObject.get("物流号").toString().replaceAll("\\s*", "");
                        if (!"".equals(logisticsNumber)) {
                            if("1".equals(order1.getPayStatus()) && "1".equals(order1.getDealStatus()) && "0".equals(order1.getRefundStatus())){
                                order1.setLogisticsNumber(logisticsNumber);
                                order1.setDealStatus("2");
                                goodsOrderTradesDao.update(order1);
                            }
                        }
                    }
                }
            }
        }
        return ResponseServer.success();
    }

}
